package controller;

import cn.hutool.json.JSONUtil;
import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

/**
 * Author：汤小洋
 * Date：2023-07-05 13:37
 * Description：<描述>
 */
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(){
        System.out.println("HelloController.hello......");

        ModelAndView mav = new ModelAndView();
        mav.addObject("name","汤小洋"); // 存在Request作用域中
        mav.setViewName("hello"); // 拼接  /view/hello.jsp

        return mav;
    }

    @RequestMapping("/hello2")
    public String hello2(Model model, Map map){
        System.out.println("HelloController.hello2......");
        // model.addAttribute("name","小跃跃"); // 存在Request作用域中
        map.put("name","小跃跃");

        // return "hello"; // 拼接  /view/hello.jsp
        // return "forward:hello"; // 转发到指定url
        return "redirect:hello"; // 重定向到指定url
    }

    @RequestMapping("/hello3")
    public void hello3(HttpServletResponse response) throws IOException {
        System.out.println("HelloController.hello3......");

        response.setContentType("application/json;charset=utf8");
        PrintWriter out = response.getWriter();
        User user = new User(1, "小洋", "123", 18, 1.88, new Date());
        out.print(JSONUtil.toJsonStr(user));
        out.close();
    }

}
